package com.example.booksystem.action;


import com.example.booksystem.bean.User;
import com.example.booksystem.biz.UserBiz;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user")
public class UserServlet extends HttpServlet {

    UserBiz userBiz = new UserBiz();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        //判断用户请求的类型
        String type = req.getParameter("type");
        switch (type){
            case "login":
                String name = req.getParameter("name");
                String pwd = req.getParameter("pwd");
                String userCode = req.getParameter("valcode");
                String code = session.getAttribute("code").toString();

                if (!code.equalsIgnoreCase(userCode)){
                    out.println("<script>alert('验证码输入错误');location.href = 'login.html';</script>");
                }else {
                    User user = userBiz.getUser(name, pwd);
                    if (user.getId()==0){
                        out.println("<script>alert('用户名或密码错误');location.href = 'login.html';</script>");
                    }else {
                        session.setAttribute("user",user);
                        out.println("<script>location.href = 'index.jsp';</script>");
                    }
                }
                break;
            case "exit":
                //清理session
                session.invalidate();
                //跳转到login.html
                out.println("<script>parent.window.location.href='login.html';</script>");
                break;
            case "modifyPwd":
                //获取新密码
                String newpwd = req.getParameter("newpwd");
                String oldpwd = req.getParameter("pwd");
                String newpwd2 = req.getParameter("newpwd2");
                //获取用户id
                User user = (User) session.getAttribute("user");
                if (!user.getPwd().equals(oldpwd)){
                    out.println("<script>alert('原密码输入错误');parent.window.location.href='index.jsp';</script>");
                }else {
                    if (!newpwd.equals(newpwd2)){
                        out.println("<script>alert('新密码两次输入不相同');parent.window.location.href='index.jsp';</script>");
                    }else {
                        int result = userBiz.modifyPwd(user.getId(), newpwd);
                        if (result>0){
                            out.println("<script>alert('密码修改成功');parent.window.location.href='login.html';</script>");
                        }else {
                            out.println("<script>alert('密码修改失败');</script>");
                        }
                    }
                }
                break;
        }

    }
}
